<?php
exit;
include_once($_SERVER["DOCUMENT_ROOT"]. "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/user/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/mblog/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/album/inc.php");
$g_cgival = array();
$g_show = array();
$g_pro = array();

if(! security_check::check_refer())
{
	throw new Exception("来路不明");
}
$_GET["recache"] = true;

function check_cgi_pro()
{
	global $g_cgival, $g_show, $g_pro;
	cgi::both($g_cgival['t'],"t","json");
	$g_cgival['t'] = string::un_html($g_cgival['t']);

	cgi::both($g_cgival['callback'], "callback", "");
	$g_cgival['callback'] = string::un_html(string::un_script_code($g_cgival['callback']));
	
	
	cgi::both($g_cgival['status_id'],"status_id",0);
	$g_cgival['status_id'] = intval($g_cgival['status_id']);
	
	cgi::both($g_cgival['album_id'],"album_id",0);
	$g_cgival['album_id'] = intval($g_cgival['album_id']);
	
	cgi::both($g_cgival['type'],"type","add");
	
}


function get_data()
{
	global $g_cgival, $g_show, $g_pro;
	
	$status_id = $g_cgival['status_id'];
	$album_id = $g_cgival['album_id'];
	
	if(!$status_id || !$album_id)
	{
		throw new Exception("数据输入错误");
	}
	
	

	$user_info = new user_info();
	$is_login = $user_info->is_logined();
	if($is_login)
	{
		$user_detail_info = $user_info->get_cur_user_info();
		
		$uid = $user_detail_info["uid"];
		
	}
	else
	{
		throw new Exception("未登录！");
	}
	
	$db_album_info = new db_album_info();
	$item = $db_album_info->get_album_item($album_id);
	if($item["create_uid"] !=$uid )
	{
		throw new Exception("无权限！");
	}
	
	
	$db_album_to_mblog = new db_album_to_mblog();
	$status_count = $db_album_to_mblog->get_album_status_count_by_album_id($album_id);
		
	
	if($g_cgival['type'] == "add")
	{
		$mod_mblog = new mod_mblog();
		$res = $mod_mblog->add_mblog_to_album($item,$album_id,$status_id,$status_count);
		
		
	}
	elseif($g_cgival['type'] == "del")
	{
		if($status_id == $item["id1"] )
		{
			$id1 = 0;
			$pic1 =0;
		}
		elseif($status_id == $item["id2"] )
		{
			$id2 = 0;
			$pic2 =0;
		}
		elseif($status_id == $item["id3"] )
		{
			$id3 = 0;
			$pic3 =0;
		}
		elseif($status_id == $item["id4"] )
		{
			$id4 = 0;
			$pic4 =0;
		}
		
		$status_count = $status_count - 1;
		$db_album_info->update_album_info_by_id($album_id,$uid,$title="",$desc="",$pic1,$pic2,$pic3,$pic4,$status_count,$status="",$show_type="",$body_url="",$body_attach="",$id1,$id2,$id3,$id4);
		
		
		$res = $db_album_to_mblog->update_album_to_mblog($album_id,$status_id,0);
		
		
	}
	elseif($g_cgival['type'] == "album_pic")
	{
		
		$db_mblog_pic_content = new db_mblog_pic_content();
		$mblog_info = $db_mblog_pic_content->get_mblog_pic_content_item($status_id);
		$pic_url  = $mblog_info["pic"];
		if($_REQUEST["pics"] == "pic1")
		{
			$id1 = $status_id;
			$pic1 =$pic_url;
		}
		elseif($_REQUEST["pics"] == "pic2")
		{
			$id2 = $status_id;
			$pic2 =$pic_url;
		}
		elseif($_REQUEST["pics"] == "pic3")
		{
			$id3 = $status_id;
			$pic3 =$pic_url;
		}
		elseif($_REQUEST["pics"] == "pic4")
		{
			$id4 = $status_id;
			$pic4 =$pic_url;
		}
		else
		{
			throw new Exception("没选择第几张图片？！");
		}
		$res = $db_album_info->update_album_info_by_id($album_id,$uid,$title="",$desc="",$pic1,$pic2,$pic3,$pic4,$status_count,$status="",$show_type="",$body_url="",$body_attach="",$id1,$id2,$id3,$id4);
		
	}
	
	if(!$res)
	{
		throw new Exception("这专辑已经添加过拉！");
	}
	else
	{
		
		
	
	}
}

 
try
{
	check_cgi_pro();
	get_data();

	$g_show["error"] = "0";
	$g_show["errmsg"] = "操作成功";
}
catch (Exception $e)
{
	$g_show["error"] = "1";
	$g_show["errmsg"] = $e->getMessage();
}
show_pro();
exit();


function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	if ($g_cgival['t'] == "serialize")
	{
		echo serialize($g_show);
	}
	elseif ($g_cgival['t'] == "json")
	{
		echo output::json("json", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "jsonp")
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "perl")
	{
		$string = php_to_perl::php_array_to_perl_hash($g_show);
		$string = mb_convert_encoding($string, "GBK", "UTF-8");
		echo $string;
	}
	elseif($g_cgival['t'] == "iframe")
	{
		header("location:{$g_cgival['link']}");
	}
	else
	{
		
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
}